#include <allegro.h>
#include <iostream>
#include <fstream>
#include "map.hpp"


cMap::cMap()
{
    size = new sPoint(0, 0);
    shift = new sPoint(0, 0);
        tex = load_bmp( "Data\\Map\\tex.bmp", default_palette );


}

void cMap::loadmap(const char * mapname)
{
                        std::fstream filemap;
                        filemap.open(mapname, std::ios::in);//otworzenie pliku



    filemap >> size->x; //wczytanie szerokosci
    filemap >> size->y;//wczytanie wysokosc
            int w = size->x,
                h = size->y;


    map = new int*[w];

        for (int i = 0; i < size->x; i++)
            map[i] = new int[h];


        for (int y=0;y<h;y++)
        for (int x=0;x<w;x++)
            filemap >> map[x][y];


filemap.close();


}



void cMap::drawmap(BITMAP * bufor, sPoint * show)
{

if (show->x + TILE_SIZE *2 >= size->x * TILE_SIZE - bufor->w) {show->x=size->x * TILE_SIZE - bufor->w - TILE_SIZE * 2-1 ;}
if (show->y + TILE_SIZE *2 >= size->y * TILE_SIZE - bufor->h) {show->y=size->y * TILE_SIZE - bufor->h - TILE_SIZE * 2   ;}


    for (int j = 0; j < bufor->h / TILE_SIZE + 2; j++)
        for (int i = 0; i < bufor->w / TILE_SIZE + 2; i++)
        {
            int x = i+show->x/TILE_SIZE;
            int y = j+show->y/TILE_SIZE;

            masked_blit(tex, bufor, map[x][y] * TILE_SIZE, 0, i * TILE_SIZE - ((int)show->x % TILE_SIZE) , j * TILE_SIZE - ((int)show->y % TILE_SIZE) , TILE_SIZE, TILE_SIZE);
        }




}




bool cMap::isOnMap(sPoint p)
{
    return p.x >= 0 && p.y >= 0 && p.x < size->x * TILE_SIZE && p.y < size->y * TILE_SIZE;
}












